<?php 
/**
*	小米推送和华为推送
*	参考的开发：https://dev.mi.com/console/doc/detail?pId=1163
*/
class xmpushChajian extends Chajian{
	
	private $api_url 	 		= 'https://api.xmpush.xiaomi.com/v2/message/alias';
	private $api_urltest 		= 'https://sandbox.xmpush.xiaomi.com/v2/message/alias';
	
	private $huawei_tokenurl 	= 'https://login.cloud.huawei.com/oauth2/v2/token';
    private $huawei_pushurl		= 'https://push-api.cloud.huawei.com/v1/{appid}/messages:send';
	
	
	//小米推送的.安卓的
	private $android_secret 	= ''; //安卓小米的secret
	private $android_package 	= ''; //包名
	
	//小米推送的.IOS苹果
	private $ios_secret 		= ''; //IOS的secret
	private $ios_bundleid 		= '';
	
	
	//华为推送的设置安卓
	private $huawei_appid 	 	= '';
	private $huawei_appsecret 	= '';
	

  

	
	protected function initChajian()
	{
		$this->android_secret 	= getconfig('xm_android_secret', $this->android_secret);
		$this->android_package 	= getconfig('xm_android_package', $this->android_package);
		
		$this->ios_secret 		= getconfig('xm_ios_secret', $this->ios_secret);
		$this->ios_bundleid 	= getconfig('xm_ios_bundleid', $this->ios_bundleid);
		
		$this->huawei_appid 	= getconfig('huawei_appid', $this->huawei_appid);
		$this->huawei_appsecret = getconfig('huawei_appsecret', $this->huawei_appsecret);
	}
	
	public function sendandroidbool()
	{
		if($this->android_secret=='')return false;
		return true;
	}
	
	public function sendiossend()
	{
		if($this->ios_secret=='')return false;
		return true;
	}
	
	public function sendhuaweisend()
	{
		if($this->huawei_appsecret=='')return false;
		return true;
	}

	/**
	*	安卓推送通知
	*/
	public function androidsend($alias, $title, $cont, $payload='')
	{
		if(!$alias)return false;
		if(is_array($alias))$alias = join(',', $alias);
		$data = array(
			'payload' => '',
			'restricted_package_name' => $this->android_package,
			'pass_through' => '0',   // 0 表示通知栏消息1表示透传消息
			'title' => $title,
			'description' => $cont,
			'alias' => $alias,
			'notify_type' => '1', //提示语就好了
			'notify_id' => rand(1,45), //可多条显示
			'extra.notify_foreground' => '1',
			'extra.notify_effect' => '1',
			'extra.badge'=>'1'
		);
		if($payload)$data['payload'] = urlencode($payload);
		return c('curl')->postcurl($this->api_url, $data, 0, array(
			'Authorization'=> 'key='.$this->android_secret
		));
	}
	
	public function iossend($alias, $title, $cont)
	{
		if(!$alias || $this->ios_secret=='')return false;
		if(is_array($alias))$alias = join(',', $alias);
		$data = array(
			'title' => $title,
			'aps_proper_fields.title' => $title,
			'description' => $cont,
			'aps_proper_fields.body' => $cont,
			'alias' => $alias,
			'extra.badge'=>'1'
		);
		return c('curl')->postcurl($this->api_url, $data, 0, array(
			'Authorization'=> 'key='.$this->ios_secret
		));
	}
	
	
	
	
	
	/**
	*	获取token
	*/
	public function hwgettoken(){
		if($this->huawei_appid=='' || $this->huawei_appsecret=='')return '';
		$token 	= c('cache')->get('hwtoken');
		if(isempt($token)){
			$result = c('curl')->postcurl($this->huawei_tokenurl, array( 
				"grant_type" => "client_credentials",
				"client_secret" => $this->huawei_appsecret,
				"client_id" => $this->huawei_appid, 
			));
			if($result){
				$barr 	= json_decode($result, true);
				$token 	= $barr['access_token'];
				c('cache')->set('hwtoken',$token, $barr['expires_in']-1);
			}
		}
		return $token;
    }
	
	/**
	*	安卓推送通知
	*/
	public function hwandroidsend($alias, $title, $cont)
	{
		$url 	= str_replace('{appid}',$this->huawei_appid, $this->huawei_pushurl);
		$token 	= $this->hwgettoken();
		if(!$token)return '';
		$data = '{"validate_only": false, "message": {"android": { "notification": { "title": "'.$title.'", "body": "'.$cont.'", "click_action": { "type": 1, "intent": "#Intent;compo=com.rvr/.Activity;S.W=U;end" } } }, "token": '.json_encode($alias).' }}';
		$result = c('curl')->postcurl($url, $data, 0, array(
			"Content-Type" => 'application/json',
             "Authorization" => "Bearer {$token}"
		));
		
		return $result;
	}
	
	
	
	/**
	*	推送，获取未发的人员
	*/
	public function push($barr, $title, $mess)
	{
		if(!$barr['success'])return;
		$datastr = $barr['data'];
		$wfuid	 = '';
		$yfuid	 = '';
		if($datastr){
			$sdata = json_decode($datastr, true);
			if(isset($sdata['pc']))$sdata = $sdata['pc'];
			if(arrvalue($sdata,'wfuid'))$wfuid.=','.$sdata['wfuid'].'';
			if(arrvalue($sdata,'yfuid'))$yfuid.=','.$sdata['yfuid'].'';
		}
		if(isset($barr['apppush'])){
			$barr = $barr['apppush'];
			if($barr['success']){
				$datastr = $barr['data'];
				if($datastr){
					$sdata = json_decode($datastr, true);
					if(isset($sdata['pc']))$sdata = $sdata['pc'];
					if(arrvalue($sdata,'wfuid'))$wfuid.=','.$sdata['wfuid'].'';
					if(arrvalue($sdata,'yfuid'))$yfuid.=','.$sdata['yfuid'].'';
				}
			}
		}
		if($wfuid=='')return;
		$wfuid = substr($wfuid, 1);
		if($yfuid!=''){
			$yfarr = explode(',', substr($yfuid, 1));
			$wfarr = explode(',',$wfuid);
			$wfuid = '';
			foreach($wfarr as $s1)if(!in_array($s1, $yfarr))$wfuid.=','.$s1.'';
			if($wfuid=='')return;
			$wfuid = substr($wfuid, 1);
		}	
		m('reim')->push('cli,xmpush', array(
			'wfuid'	=> $wfuid,
			'title'	=> $this->rock->jm->base64encode($title),
			'mess'	=> $this->rock->jm->base64encode($mess),
		));
	}
}